package com.ccx.coachsite.ui.login;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.ccx.coachsite.util.StringUtil;
import com.tencent.weibo.api.UserAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv1.OAuthV1;
import com.tencent.weibo.oauthv1.OAuthV1Client;
import com.tencent.weibo.utils.QHttpClient;
import com.tencent.weibo.webview.OAuthV1AuthorizeWebView;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;

public class SinaWeibo extends WeiboBase {

	private OAuthV1 oAuth;
	
	private static final String URL_ACTIVITY_CALLBACK = "weiboandroidsdk://TimeLineActivity";
	private static final String FROM = "xweibo";
	
	
	public SinaWeibo(Activity superActivity) {
		super(superActivity);
		loginType = 1;
		weiboTitle="新浪微博";
		oauthCallback = "null";
		oauthConsumeKey = "2310674465";
		oauthConsumerSecret = "12061ad3bba0642fd8e13a7d06c0500d";
	}
	
	public  boolean doQAuth()
	{
		boolean result = false;
		try {
			Weibo weibo = Weibo.getInstance();
			
			weibo.setupConsumerConfig(oauthConsumeKey, oauthConsumeKey);

			// Oauth2.0
			// 隐式授权认证方式
			weibo.setRedirectUrl("http://117.27.144.30/3g/callback/sina");// 此处回调页内容应该替换为与appkey对应的应用回调页
			// 对应的应用回调页可在开发者登陆新浪微博开发平台之后，
			// 进入我的应用--应用详情--应用信息--高级信息--授权设置--应用回调页进行设置和查看，
			// 应用回调页不可为空

			weibo.authorize(superActivity,
					new AuthDialogListener());
	        result = true;
		} catch (Exception e) {
			result = false;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean onQAuthResult(Intent data)
	{
		boolean result = false;
		Weibo mWeibo = Weibo.getInstance();
        String url = Weibo.SERVER + "users/show.json";
        WeiboParameters bundle = new WeiboParameters();
        bundle.add("source", Weibo.getAppKey());
//        bundle.add("access_token", Weibo.getInstance().getAccessToken()
//                .getToken());
        bundle.add("uid", weiboId);
        String response;
        try {
        	response = mWeibo.request(superActivity, url, bundle, "GET",
			        mWeibo.getAccessToken());
			JSONObject jsonObject = new JSONObject(response);
			weiboName = jsonObject.getString("name");
			weiboHeader = jsonObject.getString("profile_image_url");
			weiboId = jsonObject.getString("idstr");
            //Log.e(response);
            if(!StringUtil.isEmpty(weiboId))
            {
            	result = true;
            }
			Log.e("abc", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
        
		return result;
	}
	
	class AuthDialogListener implements WeiboDialogListener {

		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			AccessToken accessToken = new AccessToken(token, oauthConsumerSecret);
			accessToken.setExpiresIn(expires_in);
			Weibo.getInstance().setAccessToken(accessToken);
			weiboId =  values.getString("uid");
			doQAuthResult(null);
			
			
		}

		@Override
		public void onError(DialogError e) {
			Toast.makeText(superActivity,
					"授权失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onCancel() {
			Toast.makeText(superActivity, "授权取消",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onWeiboException(WeiboException e) {
			Toast.makeText(superActivity,
					"授权异常: " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}

	}
}
